<?php

use Illuminate\Database\Capsule\Manager as DB;

class ScoregoodsController extends CommonController
{
    public function indexAction()
    {
        $params = $this->get();
        $keyword = $params['keyword'] ?? '';
        $page = $params['page'] ?? 1;
        $per_page = $params['per_page'] ?? 10;

        // 获取所有积分商品
        $goods_ids = scoregoodsModel::select(['goods_id'])
            ->where('status', 'up')
            ->groupBy('goods_id')
            ->pluck('goods_id');
        $query = goodsModel::whereIn('id', $goods_ids)
            // ->where('status', 'up')
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->where('title', 'like', "%{$keyword}%")->orWhere('subtitle', 'like', "%{$keyword}%");
                }
            });

        $total = $query->count();
        $goods = $query->offset(($page - 1) * $per_page)->limit($per_page)->get();
        $score_sku_price_list = scoregoodsModel::whereIn('goods_id', $goods_ids)->where('status', 'up')->get();
        foreach ($goods as $goods_item) {
            $price_set = false;
            $item = $goods_item->toArray();
            foreach ($score_sku_price_list as $score_sku_price_item) {
                if ($score_sku_price_item->goods_id == $goods_item->id) {
                    $item['score_goods_sku_price'][] = $score_sku_price_item;
                    if (!$price_set) {
                        $item['price'] = $score_sku_price_item->score . '积分' . '+￥' . $score_sku_price_item->price;
                    }
                    // 销量
                    $item['sales'] += $score_sku_price_item->sales;
                    //$item['stock'] += $score_sku_price_item->stock;
                }
            }
            $data[] = $item;
        }

        $goods = [
            'current_page' => $page,
            'last_page' => ceil($total / $per_page),
            'per_page' => $per_page,
            'total' => $total,
            'data' => $data ?? [],
        ];

        json(['code' => 1, 'msg' => '', 'data' => $goods]);
    }

    public function detailAction()
    {
        $goods_id = $this->get('id', 0);
        $goods = DB::table('goods')->where('dianpu_id','=',0)
            ->where('id', $goods_id)
            ->where('status', 'up')
            ->first();
        if (empty($goods)) {
            throw new Exception('商品不存在或已下架');
        }

        $goods->images = explode(',', $goods->images);

        $score_goods_sku_price_list = DB::table('score_goods')
            ->where('goods_id', $goods->id)
            ->where('status', 'up')
            ->get();
        foreach ($score_goods_sku_price_list as $score_goods_sku_price_item) {
            $score_goods_sku_price_ids[] = $score_goods_sku_price_item->sku_price_id;
        }
        $score_goods_sku_price_ids = array_unique($score_goods_sku_price_ids ?? []);
        $goods_sku_price_list = DB::table('goods_sku_price')
            ->whereIn('id', $score_goods_sku_price_ids)
            ->get();
        $goods->score_goods_sku_price = $score_goods_sku_price_list;
        foreach ($goods_sku_price_list as $goods_sku_price_item) {
            $tmp_goods_sku_ids = explode(',', $goods_sku_price_item->goods_sku_ids);
            foreach ($tmp_goods_sku_ids as $tmp_goods_sku_id) {
                $goods_sku_ids[] = $tmp_goods_sku_id;
            }
        }
        $goods_sku_ids = array_unique($goods_sku_ids ?? []);
        $goods_sku_list = DB::table('goods_sku')
            ->where('goods_id', $goods->id)
            ->where(function ($query) use ($goods_sku_ids) {
                $query->where('pid', 0)->orWhereIn('id', $goods_sku_ids);
            })
            ->get();
        $goods->sku_price = [];
        $goods->stock     = 0;
        foreach ($score_goods_sku_price_list as $score_goods_sku_price_item) {
            foreach ($goods_sku_price_list as $goods_sku_price_item) {
                if ($score_goods_sku_price_item->sku_price_id == $goods_sku_price_item->id) {
                    $score_goods_sku_price_item->id = $goods_sku_price_item->id;
                    $score_goods_sku_price_item->goods_sku_id_arr = explode(',', $goods_sku_price_item->goods_sku_ids);
                    break;
                }
            }
            $goods->sku_price[] = $score_goods_sku_price_item;
            $goods->price = ($goods->price < $score_goods_sku_price_item->price)
                ? $goods->price
                : $score_goods_sku_price_item->price;
            $goods->stock += $score_goods_sku_price_item->stock;
        }
        $goods->sku = [];
        foreach ($goods_sku_list as $goods_sku_item) {
            if ($goods_sku_item->goods_id == $goods->id && $goods_sku_item->pid == 0) {
                $goods_sku_item->content = [];
                $goods->sku[] = json_decode(json_encode($goods_sku_item), true);
            }
        }
        foreach ($goods->sku as $goods_sku_key => $goods_sku_val) {
            foreach ($goods_sku_list as $goods_sku_item) {
                if ($goods_sku_item->pid == $goods_sku_val['id']) {
                    $goods->sku[$goods_sku_key]['content'][] = $goods_sku_item;
                }
            }
        }

        json(['code' => 1, 'msg' => '积分商品详情', 'data' => $goods]);
    }
}
